<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class uindex extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'frontend/layout',
        'left' => 'frontend/include/left',
        'right' => 'frontend/include/right',
    );

    function __construct() {
        parent::__construct();
        $this->load->model("scategory");
        $this->load->model("muser");
        $this->load->model("minfo");
        $this->load->model("minfocate");
        $this->load->model("mnews");
        $this->load->model("mnewscate");
        $this->load->model("mcategory");
		$this->scache->html();
    }

    public function index() {
        $data = array();
		$cat_id = 1;
		//$data = $this->getCategory();
		
		$data['position'] = 'trang-chu';
		
		$data['promotion'] = $this->minfo->getsInfo(array('INF_STATUS'=>2));
		
		$data["seo"] = $this->seo->get('trang-chu', 0);
        $this->theme_data["main"] = "frontend/uindex/home";
        $this->template->view($this->theme_data, $data);
    }
	
	
	public function view($infoCode = '') {
		$data = array();
		if($infoCode =='')
			  redirect(page404(), 301);
		$infoID = explode("-", $infoCode);
        $infoID = end($infoID);
        if (!$infoID || $infoID <= 0)
            redirect(page404(), 301);
			 
		$data['info'] = $this->minfo->get($infoID, 'INF_ID');
		if (!$data['info'])
            redirect(page404(), 301);
		
		$data["seo"] = $this->seo->set($data['info']);
		$this->theme_data["main"] = "frontend/uindex/view";
        $this->template->view($this->theme_data, $data);
	}
	
	function infocate($infoCode = '',$page = 0){
		$data = array();
		if($infoCode =='')
			  redirect(page404(), 301);
			 
		$data['infocate'] = $this->minfocate->get($infoCode, 'INFCATE_CODE');
		if (!$data['infocate'])
            redirect(page404(), 301);
		
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;

        $config['base_url'] = $this->template->url_page . $infoCode;
        $config['first_url'] = $config['base_url'] . ".html";
		

       $config['total_rows'] = $this->minfo->num(array('INFCATE_ID'=>$data['infocate']->INFCATE_ID,'INF_STATUS >'=>0));
       // $config['total_rows'] =5200;
        $this->pagination->initialize($config);

        $this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
		$data['info'] = $this->minfo->gets(array('INFCATE_ID'=>$data['infocate']->INFCATE_ID,'INF_STATUS >'=>0));
		$data["seo"] = $this->seo->set($data['infocate']);
		$this->theme_data["main"] = "frontend/uindex/infocate";
        $this->template->view($this->theme_data, $data);
	}
	
	function catenews($newsCode = '', $page = 0){
	$data = array();
		if($newsCode =='')
			redirect(page404(), 301);

		$data['infocate'] = $this->mnewscate->get($newsCode, 'NEWSCATE_CODE');
		if (!$data['infocate'])
          redirect(page404(), 301);
		
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 1;
        $config['cur_page'] = $page;

        $config['base_url'] = $this->template->url_page . 'tin-tuc/'.$newsCode;
        $config['first_url'] = $config['base_url'] . ".html";
		

       $config['total_rows'] = $this->mnews->num(array('NEWSCATE_ID'=>$data['infocate']->NEWSCATE_ID,'NEWS_STATUS >'=>0));
        $this->pagination->initialize($config);

        $this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
		$data['news'] = $this->mnews->gets(array('NEWSCATE_ID'=>$data['infocate']->NEWSCATE_ID,'NEWS_STATUS >'=>0));
		$data["seo"] = $this->seo->set($data['infocate']);
		$this->theme_data["main"] = "frontend/uindex/newscate";
        $this->template->view($this->theme_data, $data);
	}
	
	public function viewnews($newsCode = '') {
		$data = array();
		if($newsCode =='')
			  redirect(page404(), 301);
		$newsID = explode("-", $newsCode);
        $newsID = end($newsID);
        //if (!$newsID || $newsID <= 0)
            //redirect(page404(), 301);
			 
		$data['news'] = $this->mnews->get($newsID, 'NEWS_ID');
		//if (!$data['news'])
           // redirect(page404(), 301);
		
		$data["seo"] = $this->seo->set($data['news']);
		$this->theme_data["right"] = null;
		$this->theme_data["main"] = "frontend/uindex/viewnews";
        $this->template->view($this->theme_data, $data);
	}
	
	function contact(){
		$data = array();
		$post = $this->input->post();
 
		if($post){
			$name = @$post['FULLNAME'];
			$phone = @$post['phone'];
			$goicuoc = @$post['goicuoc'];
			$content = @$post['CONTENT'];
			if(!$this->mail_sendContact($name,$phone, $goicuoc,$content))
			die("Khong gui duoc mail, vui long thu lai");
		}
		$data['promotion'] = $this->minfo->gets(array('INF_STATUS'=>2));
		$data["seo"] = $this->seo->get('trang-chu', 0);
		$this->theme_data["right"] = null;
		$this->theme_data["main"] = "frontend/uindex/contact";
        $this->template->view($this->theme_data, $data);
	}
	

	//email gửi mật khẩu về
    function mail_sendContact($name, $phone = '', $tengoicuoc='', $content = '') {
	
        $this->load->library('email');
		  $this->email->clear();
        $this->email->from('info@capquangfpthcm.net', 'info@capquangfpthcm.net');

       $this->email->to('khavh@fpt.com.vn');
       $this->email->bcc('st.trunghieu@gmail.com');
        $this->email->subject('Thông tin khách hàng đăng ký capquangfpthcm.net');
        $this->email->message('
Chào bạn,
Có khách hàng tên là: '.$name.'
Số điện thoại: '.$phone.'
Đăng ký gói cước: '.$tengoicuoc.' 
Lúc '.date("d/m/Y H:i:s", time()).'
Với lời nhắn như sau: 
 '.$content.' 
 Trân trọng');
      return $this->email->send();
	   
	}
	function page404(){
		$data = array();
//		$data = $this->getCategory();
		$this->output->set_status_header('404');
		set_status_header(404);
		$this->theme_data["left"] = null;
		$this->theme_data["right"] = null;
		$this->theme_data["main"] = "frontend/include/page404";
        $this->template->view($this->theme_data, $data);
	}
}
